package edu.hunau.service.impl;

import edu.hunau.dao.UserDao;
import edu.hunau.exception.BusinessException;
import edu.hunau.exception.UsernameOrPasswordErrorException;
import edu.hunau.pojo.User;
import edu.hunau.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author hb
 * @date 2023-04-14 14:41:18
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    @Override
    public User login(String username, String pwd) throws UsernameOrPasswordErrorException {
        System.out.println("UserServiceImpl#login(String,String) ...");
        User user= userDao.queryByUsernameAndPassword(username,pwd);
        if(user == null){
            throw new UsernameOrPasswordErrorException("用户名或者密码错误！");
        }

        return user;
    }

    @Override
    public void regist(User user) {
            System.out.println("UserServiceImpl#regist(User) ...");
           userDao.insert(user);
    }

    @Override
    public void checkUsername(String username) throws BusinessException {
        System.out.println("UserServiceImpl#checkUsername(String) ...");
        User user = userDao.queryByUserName(username);
        if(user!=null){
            throw new BusinessException("用户存在！");
        }
    }
}
